package com.example.rlw.mvpplugintest.jd;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.RelativeLayout;

import com.example.rlw.mvpplugintest.R;
import com.example.rlw.mvpplugintest.adapter.MyRecyclerviewAdapter;
import com.example.rlw.mvpplugintest.mvp.MVPBaseActivity;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 描述：仿京东首页 瀑布流
 * Created by rlw on 2019/3/21.
 */
public class WaterfallActivity extends MVPBaseActivity {
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.head_search_rr)
    RelativeLayout mHeadSearchRr;

    private int mDistanceY;
    private StaggeredGridLayoutManager staggeredGridLayoutManager;

    @Override
    protected int getLayoutId() {
        return R.layout.water_fall_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("O(∩_∩)O哈哈~" + i);
        }
        staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
        //设置空隙处理方式为不处理--item乱跳问题
        staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        mRecyclerview.setLayoutManager(staggeredGridLayoutManager);
        MyRecyclerviewAdapter adapter = new MyRecyclerviewAdapter(this, list);
        mRecyclerview.setAdapter(adapter);

        mRecyclerview.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //防止item乱跳
                //staggeredGridLayoutManager.invalidateSpanAssignments();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //滑动的距离
                mDistanceY += dy;
                //toolbar的高度
                int toolbarHeight = mHeadSearchRr.getBottom();
                //当滑动的距离 <= toolbar高度的时候，改变Toolbar背景色的透明度，达到渐变的效果
                if (mDistanceY <= toolbarHeight) {
                    float scale = (float) mDistanceY / toolbarHeight;
                    float alpha = scale * 255;
                    mHeadSearchRr.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                } else {
                    //将标题栏的颜色设置为完全不透明状态
                    mHeadSearchRr.setBackgroundResource(R.color.white);
                }
            }
        });
    }

    @Override
    protected void initNet() {

    }
}
